package com.zjlanyun.erp;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListPopupWindow;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;

import com.zjlanyun.erp.utils.UpdateManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CMainActivity extends Activity {

	private Context mContext = this;
	private LinearLayout ll_menu;
	private String[] menuTitles = { "连接设置", "检查更新", "注销" };
	private int[] menuImages = { R.drawable.icon_connect_setup, R.drawable.ic_cached_black_24dp,
			R.drawable.icon_logout };
	private ListPopupWindow mListPopupWindow;
	private RelativeLayout order;
	private UpdateManager mUpdateManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.c_main_activity);

		// 检查更新
//		mUpdateManager = new UpdateManager(mContext);
//		mUpdateManager.checkUpdateInfo();

		initView();
		initMenu();
	}

	// 初始化view
	private void initView() {
		ll_menu = (LinearLayout) findViewById(R.id.ll_menu);
		order = (RelativeLayout) findViewById(R.id.order);

		// 打开右上角菜单
		ll_menu.setOnClickListener(new View.OnClickListener() {
			@SuppressLint("NewApi")
			@Override
			public void onClick(View arg0) {
				mListPopupWindow.setAnchorView(arg0);
				mListPopupWindow.show();
			}
		});

		// 销售订单
		order.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

			}
		});
	}

	// 初始化菜单
	@SuppressLint("NewApi")
	private void initMenu() {
		mListPopupWindow = new ListPopupWindow(this);
		mListPopupWindow.setAdapter(new SimpleAdapter(this, getMenuData(), R.layout.menu_item,
				new String[] { "image", "title" }, new int[] { R.id.image, R.id.title }));
		;
		mListPopupWindow.setWidth(Common.dp2px(170, mContext));
		mListPopupWindow.setHeight(LayoutParams.WRAP_CONTENT);
		mListPopupWindow.setModal(true);
		mListPopupWindow.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				mListPopupWindow.dismiss();
				if (position == 0) {
					Intent intent = new Intent(CMainActivity.this, ConnectSetup.class);
					startActivity(intent);
				} else if (position == 1) {
					mUpdateManager.manualCheckUpdate();
				} else if (position == 2) {
					logout();
				}
			}
		});
	}

	// 初始化菜单数据
	private List<Map<String, Object>> getMenuData() {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Map<String, Object> map;
		for (int i = 0; i < menuTitles.length; i++) {
			map = new HashMap<String, Object>();
			map.put("image", menuImages[i]);
			map.put("title", menuTitles[i]);
			list.add(map);
		}
		return list;
	}

	// 初始化注销
	private void logout() {
		AlertDialog.Builder builder = new Builder(mContext);
		builder.setMessage("确定注销当前用户吗？");
		builder.setTitle("提示");
		builder.setPositiveButton("注销", new OnClickListener() {
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				Common.logout(mContext); // 调用清空用户资料
				finish();
			}
		});
		builder.setNegativeButton("取消", new OnClickListener() {
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
			}
		});
		builder.create().show();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			logout();
		}
		return false;
	}
}
